//
//  PickNumCQSSViewController.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-23.
//
//

#import "JddPickNumCQSSViewController.h"
#import "JddViewControllerManager.h"
#import "NoticeViewController.h"
#import "FloatLayer.h"

#define kFirstComponent 0
#define kSubComponent 1
@interface JddPickNumCQSSViewController (){
    NSInteger _floatLayerIndex;
    FloatLayer *_layer;
    NSArray *_plistArr;
    NSMutableArray *_cqssArr;
}

@end

@implementation JddPickNumCQSSViewController

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddPickNumCQSSViewController);

@synthesize playIDnameLabel;
@synthesize playIDTips;
@synthesize issueCodeLabel;
@synthesize remainTimeLabel;
@synthesize zhushuLabel;
@synthesize moneyLabel;
@synthesize currentLotteryIssue;

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.message isEqualToString:@"您还没有登录，请先登录"]) {
        if (buttonIndex == 0) {
            zhushuLabel.text = @"0注";
            moneyLabel.text = @"0彩豆";
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN fromController:self withObject:nil withObject2:nil];
        }
    }
}

-(void)numberButtonClick:(id)sender{
    NSString *subPlayName = [playName objectAtIndex:1];
    BOOL isFS = NO;
    if ([subPlayName isEqualToString:@"复选"]) {
        isFS = YES;
    }
    UIButton *button = sender;
    //tag=66||77，玩法=复选，一组里则只能单选
    if(isFS || (button.tag == 66 ||button.tag == 77)){
        [self singleSelectedAction:button];
    }
    [button setSelected:![button isSelected]];
    if (button.tag == 11) {
        if ([button isSelected]) {
            [lotteryNumber addBallNumber:[button.titleLabel.text intValue]];
        }else{
            [lotteryNumber removeBallNumber:[button.titleLabel.text intValue]];
        }
    }else if (button.tag == 22){
        if ([button isSelected]) {
            [lotteryNumber add2BallNumber:[button.titleLabel.text intValue]];
        }else{
            [lotteryNumber remove2BallNumber:[button.titleLabel.text intValue]];
        }
    }else if (button.tag == 33){
        if ([button isSelected]) {
            [lotteryNumber add3BallNumber:[button.titleLabel.text intValue]];
        }else{
            [lotteryNumber remove3BallNumber:[button.titleLabel.text intValue]];
        }
    }else if (button.tag == 44){
        if ([button isSelected]) {
            [lotteryNumber add4BallNumber:[button.titleLabel.text intValue]];
        }else{
            [lotteryNumber remove4BallNumber:[button.titleLabel.text intValue]];
        }
    }else if (button.tag == 55){
        if ([button isSelected]) {
            [lotteryNumber add5BallNumber:[button.titleLabel.text intValue]];
        }else{
            [lotteryNumber remove5BallNumber:[button.titleLabel.text intValue]];
        }
    }else if (button.tag == 66){
        NSInteger value = [JddCommonFunction getNumberByStrDXDS:button.titleLabel.text];
        if ([button isSelected]) {
            [lotteryNumber add6BallNumber:value];
        }else{
            [lotteryNumber remove6BallNumber:value];
        }
    }else if (button.tag == 77){
        NSInteger value = [JddCommonFunction getNumberByStrDXDS:button.titleLabel.text];
        if ([button isSelected]) {
            [lotteryNumber add7BallNumber:value];
        }else{
            [lotteryNumber remove7BallNumber:value];
        }
    }
    if ([button isSelected]) {
        [JddCommonFunction animationViewWithZoomOutIn:button delaySecond:0];
    }
    [self countMoney];
}

- (void)singleSelectedAction:(UIButton *)btn1
{
    for (UIView *subView in sc.subviews) {
        if (subView.tag == btn1.tag) {
            UIButton *btn2 = (UIButton *)subView;
            if (btn1 != btn2) {
                btn2.selected = NO;
                if (btn2.tag == 11) {
                    [lotteryNumber removeBallNumber:[btn2.titleLabel.text intValue]];
                }else if (btn2.tag == 22) {
                    [lotteryNumber remove2BallNumber:[btn2.titleLabel.text intValue]];
                }else if (btn2.tag == 33) {
                    [lotteryNumber remove3BallNumber:[btn2.titleLabel.text intValue]];
                }else if (btn2.tag == 44) {
                    [lotteryNumber remove4BallNumber:[btn2.titleLabel.text intValue]];
                }else if (btn2.tag == 55) {
                    [lotteryNumber remove5BallNumber:[btn2.titleLabel.text intValue]];
                }else if (btn2.tag == 66) {
                    NSInteger value = [JddCommonFunction getNumberByStrDXDS:btn2.titleLabel.text];
                    [lotteryNumber remove6BallNumber:value];
                }else if (btn2.tag == 77) {
                    NSInteger value = [JddCommonFunction getNumberByStrDXDS:btn2.titleLabel.text];
                    [lotteryNumber remove7BallNumber:value];
                }
            }
        }
    }
}

-(void)countMoney{
    NSInteger tempCount = 0;
    NSString *firstPlayName = [playName objectAtIndex:0];
    NSString *subPlayName = [playName objectAtIndex:1];
    BOOL isFS = NO;
    if ([subPlayName isEqualToString:@"复选"]) {
        isFS = YES;
    }
    BOOL isZX = NO;
    if([subPlayName isEqualToString:@"组选"]){
        isZX = YES;
    }
    if ([firstPlayName isEqualToString:@"五星"]) {
        tempCount = [[lotteryNumber BallArray] count]*[[lotteryNumber SecondBallArray] count]*[[lotteryNumber ThirdBallArray] count]*[[lotteryNumber FourthBallArray] count]*[[lotteryNumber FifthBallArray] count];
        if (isFS) {
            tempCount *= 4;
        }
    }else if ([firstPlayName isEqualToString:@"三星"]){
        tempCount = [[lotteryNumber ThirdBallArray] count]*[[lotteryNumber FourthBallArray] count]*[[lotteryNumber FifthBallArray] count];
        if (isFS) {
            tempCount *= 3;
        }
    }else if ([firstPlayName isEqualToString:@"二星"]){
         tempCount = [[lotteryNumber FourthBallArray] count]*[[lotteryNumber FifthBallArray] count];
        if (isFS) {
            tempCount *= 2;
        }else if(isZX){
            switch ([[lotteryNumber FifthBallArray] count]) {
                case 1:
                    tempCount = 0;
                    break;
                case 2:
                    tempCount = 1;
                    break;
                case 3:
                    tempCount = 3;
                    break;
                case 4:
                    tempCount = 6;
                    break;
                case 5:
                    tempCount = 10;
                    break;
                case 6:
                    tempCount = 15;
                    break;
                case 7:
                    tempCount = 21;
                    break;
                case 8:
                    tempCount = 28;
                    break;
                case 9:
                    tempCount = 36;
                    break;
                case 10:
                    tempCount = 45;
                    break;
                default:
                    break;
            }
        }
    }else if ([firstPlayName isEqualToString:@"一星"]){
         tempCount = 1 * [[lotteryNumber FifthBallArray] count];
    }else{
    tempCount = [[lotteryNumber SixthBallArray] count]*[[lotteryNumber SeventhBallArray] count];
    }
    
    zhushuLabel.text = [NSString stringWithFormat:@"%d注",tempCount];
    moneyLabel.text = [NSString stringWithFormat:@"%d彩豆",tempCount*2];
}

-(void)setScroViewContentOffsetWithView:(UIView *)view
{
    CGPoint pt;
    CGRect rc =[view bounds];
    rc =[view convertRect:rc toView:sc];
    pt = rc.origin;
    [sc setContentOffset:pt animated:YES];
    if (view != bg1) {
        sc.scrollEnabled = NO;
    }
    defaultBg = view;
}

-(void)switchDXDSAndStartWithIsHidden:(BOOL)isHidden
{
    for (UIView *subView in sc.subviews) {
        if (subView.tag == 44 || subView.tag == 55) {
            subView.hidden = isHidden;
        }
        if (subView.tag == 66 || subView.tag == 77) {
            [subView setExclusiveTouch:YES];
            subView.hidden = !isHidden;
        }
    }
}

-(void)setBallBtnStatus
{
    for (UIView *subView in sc.subviews) {
        if (subView.tag == 11 ||subView.tag == 22 || subView.tag == 33 || subView.tag == 44 || subView.tag == 55 || subView.tag == 66 || subView.tag == 77) {
            UIButton *btn = (UIButton *)subView;
            btn.selected = NO;
        }
    }
}

-(void)listSelect{
//    CGRect rect = [[UIScreen mainScreen] bounds];
//    rect.origin.x = (rect.size.width) / 2.0;
//    rect.origin.y = (rect.size.height) / 2.0;
//    rect.size.width = 251.0;
//    rect.size.height = 300.0;
//    indexList = [JddIndexListView getInstanceView];
//    [indexList setFrame:rect];
//    indexList.type = 0;
//    indexList.delegate = self;
//    [indexList show];
    NoticeViewController *noticeView = [[NoticeViewController alloc] init];
    noticeView.webInfoId = LOTTERY_TYPE_ID_CQSSC;
    [self.navigationController pushViewController:noticeView animated:YES];
}

//-(void)selectorbutton1{
//    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOTTERY_OPEN_LIST
//                                                         fromController:self
//                                                             withObject:[NSString stringWithFormat:@"%d", LOTTERY_TYPE_ID_CQSS]
//                                                            withObject2:nil];
//}
//
//-(void)selectorbutton2{
//    if (![JddCommonFunction isLogin]) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球"
//                                                        message:@"您还没有登录，请先登录"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:@"取消", nil]
//        ;
//        [alert show];
//    }else {
//        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BUY_HISTORY
//                                                             fromController:self
//                                                                 withObject:[NSNumber numberWithInt:LOTTERY_TYPE_ID_CQSS]
//                                                                withObject2:nil];
//    }
//}
//
//-(void)selectorbutton3{
//    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_HOW_TO_PLAY_SSQ
//                                                         fromController:self
//                                                             withObject:nil
//                                                            withObject2:nil];
//}
//
//-(void)selectorbutton4{
//    if (![JddCommonFunction isLogin]) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球"
//                                                        message:@"您还没有登录，请先登录"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:@"取消", nil]
//        ;
//        [alert show];
//    }else {
//        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_FOLLOW_NUMBER_HISTORY
//                                                             fromController:self
//                                                                 withObject:nil
//                                                                withObject2:nil];
//    }
//}

- (void)QueryDigitalLotteryIssueOperationResult:(NSData *)result{
    if (result) {
        if ([JddDataParser parseLotteryIssueResult:result lotteryIssue:currentLotteryIssue]){
            issueCodeLabel.text = [NSString stringWithFormat:@"第%@期",currentLotteryIssue.name];
            endTime = [JddCommonFunction dateFromString:currentLotteryIssue.endTime];
            currentTime = [JddCommonFunction dateFromString:currentLotteryIssue.currentTime];
            [countDownTimer invalidate];
            countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                              target:self
                                                            selector:@selector(countDownTimer:)
                                                            userInfo:nil
                                                             repeats:YES];
        }
    }
}

- (void)countDownTimer:(NSTimer*)thisTimer{
    currentTime = [currentTime dateByAddingTimeInterval:1];
    NSTimeInterval interval = [endTime timeIntervalSinceDate:currentTime];
    if (interval <= 1) {
        interval = 0;
        [self startQuery];
        return;
        
    } else if(interval>1) {
        NSInteger remainDay = interval / 86400;
        NSInteger remainHour = (interval - 86400 * remainDay) / 3600;
        NSInteger remainMinute = (interval - 86400 * remainDay - remainHour * 3600) / 60;
        NSInteger remainSecond = (interval - 86400 * remainDay - remainHour *3600 - remainMinute * 60);
        remainTimeLabel.text = [NSString stringWithFormat:@"%d天%d小时%d分%d秒", remainDay, remainHour, remainMinute, remainSecond];
    }
}

-(void)startQuery{
    JddQueryDigitalLotteryIssueOperation *operation = [[JddQueryDigitalLotteryIssueOperation alloc] init];
    operation.delegate = self;
    operation.lotteryTypeId = [NSString stringWithFormat:@"%d", LOTTERY_TYPE_ID_CQSS];
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}


//设置彩种，后进行刷新数据
- (void)setLotteryContent:(NSInteger)tag withFirst:(NSString *)first withLast:(NSString *)last{
    
    
    _floatLayerIndex = tag;
    
    
    NSString * firstString = first;
    NSString * subString = last;
    
    playName = [NSArray arrayWithObjects:firstString, subString, nil];
    NSString *str = [NSString string];
    self.label5.text = @"个位";
    //大小单双
    if ([firstString isEqualToString:@"大小单双"]) {
        str = @"从个、十位中任选一种性质组成一注投注号码、奖金4彩豆";
        //滚动
        [self setScroViewContentOffsetWithView:bg3];
        //显示大小单球，隐藏其它种类
        [self switchDXDSAndStartWithIsHidden:YES];
    }
    else{
        //显示其它球种，隐藏大小单双
        [self switchDXDSAndStartWithIsHidden:NO];
        BOOL isFS = NO;
        if ([subString isEqualToString:@"复选"]) {
            isFS = YES;
        }
        if ([firstString isEqualToString:@"五星"]) {
            if ([subString isEqualToString:@"直选"]) {
                str= @"选号与开奖号码全部相同且顺序一致，奖金100000彩豆。";
            }else if ([subString isEqualToString:@"通选"]) {
                str= @"一注号码，三个奖级通吃，五次中奖机会，大奖20440彩豆。";
            }else if (isFS) {
                str= @"包括五星、三星、二星、一星各投一注，最高奖金达101110（为五星奖金+三星奖金+二星奖金+一星奖金）彩豆。";
            }
            //滚动
            [self setScroViewContentOffsetWithView:bg1];
            sc.scrollEnabled = YES;
        }else if ([firstString isEqualToString:@"三星"]){
            if ([subString isEqualToString:@"直选"]) {
                str= @"选号与开奖号码全部相同且顺序一致，奖金1000彩豆。";
            }else if (isFS) {
                str= @"包括三星、二星、一星各投一注，最高奖金达1110彩豆。";
            }
            //滚动
            [self setScroViewContentOffsetWithView:bg2];
        }else if ([firstString isEqualToString:@"二星"]){
            if ([subString isEqualToString:@"直选"]) {
                str= @"选号与开奖号码全部相同且顺序一致，奖金100彩豆。";
            }else if (isFS) {
                str= @"包括二星、一星各投一注，最高奖金达110彩豆。";
            }
            if ([subString isEqualToString:@"组选"]) {
                str= @"猜开奖号码的最后两位，选择2个或以上号码投注，奖金50彩豆。";
                //滚动
                [self setScroViewContentOffsetWithView:bg4];
                self.label5.text = @"个位";
            }else{
                //滚动
                [self setScroViewContentOffsetWithView:bg3];
            }
        }else if ([firstString isEqualToString:@"一星"]){
            str = @"选号与开奖号码全部相同且顺序一致，奖金10彩豆";
            //滚动
            [self setScroViewContentOffsetWithView:bg4];
        }
    }
    playIDTips.text = str;
    playIDnameLabel.text = [NSString stringWithFormat:@"%@%@", firstString, subString];
    [self clearButtonClick:nil];
    [self setRbtnTitle:[NSString stringWithFormat:@"%@-%@",PAGE_CQSS_PICK_NUMBER,playIDnameLabel.text]];
    
    [_layer loadFloatLayer:_cqssArr withIndex:_floatLayerIndex];
}

//显示下面的pickView
-(IBAction)changePlayIDClick:(id)sender{
    if (!_layer) {
        _plistArr = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CQSSPlayNames.plist" ofType:nil]];
        _cqssArr = [[NSMutableArray alloc] init];
        
        for (NSInteger i=0; i<_plistArr.count; i++) {
            NSArray *playArr = [[_plistArr objectAtIndex:i] objectForKey:@"playNames"];
            for (NSInteger j=0; j<[playArr count]; j++) {
                [_cqssArr addObject:[NSString stringWithFormat:@"%@%@",[[_plistArr objectAtIndex:i] objectForKey:@"type"],[playArr objectAtIndex:j]]];
            }
            
        }
        
        _layer = [[FloatLayer alloc] initWithFloatLayer:FloatLayerCQSS withLayerCQSSCallBack:^(NSInteger index, NSString *firstStr, NSString *lastStr) {
            [self setLotteryContent:index withFirst:firstStr withLast:lastStr];
        }];
    }
    
    [_layer loadFloatLayer:_cqssArr withIndex:_floatLayerIndex];
    
    
    /*
    BOOL isExist = NO;
    for(UIView *u in self.view.subviews){
        if ([u isKindOfClass:[TSLocateView class]]) {
            isExist = YES;
            break;
        }
    }
    if (!isExist) {
        TSLocateView *locateView = [[TSLocateView alloc] initWithTitle:@"重庆时时彩玩法选择" AndState:[playName objectAtIndex:0] AndCity:[playName objectAtIndex:1] delegate:self ];
        locateView.backgroundColor = [UIColor whiteColor];
        [locateView showInView:self.view];
    }
    */
}

-(void)selectPlayMethodButtonClick{
    [self changePlayIDClick:nil];
}

- (IBAction)clearButtonClick:(id)sender {
    [lotteryNumArray removeAllObjects];
    [lotteryNumber clear];
    zhushuLabel.text = @"0注";
    moneyLabel.text = @"0彩豆";
    //重置所有球按钮为不选中
    [self setBallBtnStatus];
}
- (void)RandomSelectNumViewClickedButtonAtIndex:(NSNumber*)buttonIndex multiple:(NSNumber*)multiple{
    NSString *firstPlayName = [playName objectAtIndex:0];
    NSString *subPlayName = [playName objectAtIndex:1];

    if ([buttonIndex intValue]== 1) {
        [self clearButtonClick:nil];
        for (int i = 0; i < [multiple intValue]; ++i) {
            [lotteryNumber clear];
            if ([firstPlayName isEqualToString:@"五星"]) {
                [lotteryNumber addBallNumber:arc4random()%10];
                [lotteryNumber add2BallNumber:arc4random()%10];
                [lotteryNumber add3BallNumber:arc4random()%10];
                [lotteryNumber add4BallNumber:arc4random()%10];
                [lotteryNumber add5BallNumber:arc4random()%10];
            }else if ([firstPlayName isEqualToString:@"三星"]){
                [lotteryNumber add3BallNumber:arc4random()%10];
                [lotteryNumber add4BallNumber:arc4random()%10];
                [lotteryNumber add5BallNumber:arc4random()%10];
            }else if ([firstPlayName isEqualToString:@"二星"]){
                if ([subPlayName isEqualToString:@"组选"]) {
                    int aNum, bNum;
                    do{
                    aNum = arc4random()%10;
                    bNum = arc4random()%10;
                    if (aNum != bNum) {
                        [lotteryNumber add5BallNumber:aNum];
                        [lotteryNumber add5BallNumber:bNum];
                    }
                    }while(aNum == bNum);
                }else{
                 [lotteryNumber add4BallNumber:arc4random()%10];
                [lotteryNumber add5BallNumber:arc4random()%10];
                    }
            }else if ([firstPlayName isEqualToString:@"一星"]){
                [lotteryNumber add5BallNumber:arc4random()%10];
            }else{
                [lotteryNumber add6BallNumber:arc4random()%4];
                [lotteryNumber add7BallNumber:arc4random()%4];
            }
            JddFc3dNumber *newNum = [[JddFc3dNumber alloc] init];
            [newNum.BallArray addObjectsFromArray:lotteryNumber.BallArray];
            [newNum.SecondBallArray addObjectsFromArray:lotteryNumber.SecondBallArray];
            [newNum.ThirdBallArray addObjectsFromArray:lotteryNumber.ThirdBallArray];
            [newNum.FourthBallArray addObjectsFromArray:lotteryNumber.FourthBallArray];
            [newNum.FifthBallArray addObjectsFromArray:lotteryNumber.FifthBallArray];
            [newNum.SixthBallArray addObjectsFromArray:lotteryNumber.SixthBallArray];
            [newNum.SeventhBallArray addObjectsFromArray:lotteryNumber.SeventhBallArray];
            [lotteryNumArray addObject:newNum];
        }
        NSString *name = [NSString stringWithFormat:@"%@%@", [playName objectAtIndex:0], [playName objectAtIndex:1]];
        NSMutableArray *lotteryNumArrayTemp = [NSMutableArray arrayWithArray:lotteryNumArray];
        [lotteryNumber clear];
        [lotteryNumArray removeAllObjects];
        isQuit = FALSE;
         [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_CQSS_LIST fromController:self withObject:lotteryNumArrayTemp withObject2:name];
    }
}
- (IBAction)randomButtonClick:(id)sender {
//    CGRect rect = [[UIScreen mainScreen] bounds];
//    rect.origin.x = (rect.size.width - 251) / 2.0;
//    rect.origin.y = (rect.size.height - 139) / 2.0;
//    rect.size.width = 251.0;
//    rect.size.height = 139.0;
//    randomSelectNumberDialog = [JddRandomSelectNumView getInstanceView];
//    [randomSelectNumberDialog setFrame:rect];
//    randomSelectNumberDialog.type =1;
//    randomSelectNumberDialog.delegate = self;
//    [randomSelectNumberDialog show];
    NSString *firstPlayName = [playName objectAtIndex:0];
    NSString *subPlayName = [playName objectAtIndex:1];
    [self clearButtonClick:nil];
    for (int i = 0; i < 5; ++i) {
        [lotteryNumber clear];
        if ([firstPlayName isEqualToString:@"五星"]) {
            [lotteryNumber addBallNumber:arc4random()%10];
            [lotteryNumber add2BallNumber:arc4random()%10];
            [lotteryNumber add3BallNumber:arc4random()%10];
            [lotteryNumber add4BallNumber:arc4random()%10];
            [lotteryNumber add5BallNumber:arc4random()%10];
        }else if ([firstPlayName isEqualToString:@"三星"]){
            [lotteryNumber add3BallNumber:arc4random()%10];
            [lotteryNumber add4BallNumber:arc4random()%10];
            [lotteryNumber add5BallNumber:arc4random()%10];
        }else if ([firstPlayName isEqualToString:@"二星"]){
            if ([subPlayName isEqualToString:@"组选"]) {
                int aNum, bNum;
                do{
                    aNum = arc4random()%10;
                    bNum = arc4random()%10;
                    if (aNum != bNum) {
                        [lotteryNumber add5BallNumber:aNum];
                        [lotteryNumber add5BallNumber:bNum];
                    }
                }while(aNum == bNum);
            }else{
                [lotteryNumber add4BallNumber:arc4random()%10];
                [lotteryNumber add5BallNumber:arc4random()%10];
            }
        }else if ([firstPlayName isEqualToString:@"一星"]){
            [lotteryNumber add5BallNumber:arc4random()%10];
        }else{
            [lotteryNumber add6BallNumber:arc4random()%4];
            [lotteryNumber add7BallNumber:arc4random()%4];
        }
        JddFc3dNumber *newNum = [[JddFc3dNumber alloc] init];
        [newNum.BallArray addObjectsFromArray:lotteryNumber.BallArray];
        [newNum.SecondBallArray addObjectsFromArray:lotteryNumber.SecondBallArray];
        [newNum.ThirdBallArray addObjectsFromArray:lotteryNumber.ThirdBallArray];
        [newNum.FourthBallArray addObjectsFromArray:lotteryNumber.FourthBallArray];
        [newNum.FifthBallArray addObjectsFromArray:lotteryNumber.FifthBallArray];
        [newNum.SixthBallArray addObjectsFromArray:lotteryNumber.SixthBallArray];
        [newNum.SeventhBallArray addObjectsFromArray:lotteryNumber.SeventhBallArray];
        [lotteryNumArray addObject:newNum];
    }
    NSString *name = [NSString stringWithFormat:@"%@%@", [playName objectAtIndex:0], [playName objectAtIndex:1]];
    NSMutableArray *lotteryNumArrayTemp = [NSMutableArray arrayWithArray:lotteryNumArray];
    [lotteryNumber clear];
    [lotteryNumArray removeAllObjects];
    isQuit = FALSE;
    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_CQSS_LIST fromController:self withObject:lotteryNumArrayTemp withObject2:name];
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
    static NSDate *shakeStart;
    NSDate *now = [[NSDate alloc] init];
    NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];
    if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil){
        shakeCount = 0;
        shakeStart = [[NSDate alloc] init];
    }
    
    if (!sharked&&(fabsf(acceleration.x) > 1.0 || fabsf(acceleration.y) > 1.0 || fabsf(acceleration.z) > 1.1)){
        shakeCount++;
    }
    
    if (shakeCount > 0){
        shakeCount = 0;
        shakeStart = [[NSDate alloc] init];
        [self yaohao];

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
}

#pragma mark 摇一摇
-(void)yaohao
{
    NSString *subPlayName = [playName objectAtIndex:1];
    NSString *firstPlayName = [playName objectAtIndex:0];
    [self clearButtonClick:nil];
    if ([firstPlayName isEqualToString:@"五星"]) {
        [lotteryNumber addBallNumber:arc4random()%10];
        [lotteryNumber add2BallNumber:arc4random()%10];
        [lotteryNumber add3BallNumber:arc4random()%10];
        [lotteryNumber add4BallNumber:arc4random()%10];
        [lotteryNumber add5BallNumber:arc4random()%10];
        for (UIView* inView in sc.subviews) {
            if (inView.tag == 11 && [lotteryNumber BallHasNumber:[((UIButton*)inView).titleLabel.text intValue]]) {
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 22 && [lotteryNumber Ball2HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 33 && [lotteryNumber Ball3HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 44 && [lotteryNumber Ball4HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 55 && [lotteryNumber Ball5HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }
        }
    }else if ([firstPlayName isEqualToString:@"三星"]){
        [lotteryNumber add3BallNumber:arc4random()%10];
        [lotteryNumber add4BallNumber:arc4random()%10];
        [lotteryNumber add5BallNumber:arc4random()%10];
        for (UIView* inView in sc.subviews) {
            if (inView.tag == 33 && [lotteryNumber Ball3HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 44 && [lotteryNumber Ball4HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 55 && [lotteryNumber Ball5HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }
        }
    }else if ([firstPlayName isEqualToString:@"二星"]){
        if ([subPlayName isEqualToString:@"组选"]) {
            int aNum, bNum;
            do{
                aNum = arc4random()%10;
                bNum = arc4random()%10;
                if (aNum != bNum) {
                    [lotteryNumber add5BallNumber:aNum];
                    [lotteryNumber add5BallNumber:bNum];
                }
            }while(aNum == bNum);
        }else{
            [lotteryNumber add4BallNumber:arc4random()%10];
            [lotteryNumber add5BallNumber:arc4random()%10];
        }
        for (UIView* inView in sc.subviews) {
            if (inView.tag == 44 && [lotteryNumber Ball4HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 55 && [lotteryNumber Ball5HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }
        }
    }else if ([firstPlayName isEqualToString:@"一星"]){
        [lotteryNumber add5BallNumber:arc4random()%10];
        for (UIView* inView in sc.subviews) {
            if (inView.tag == 55 && [lotteryNumber Ball5HasNumber:[((UIButton*)inView).titleLabel.text intValue]]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }
        }
    }else{
        [lotteryNumber add6BallNumber:arc4random()%4];
        [lotteryNumber add7BallNumber:arc4random()%4];
        for (UIView* inView in sc.subviews) {
            if (inView.tag == 66 || inView.tag == 77) {
            NSInteger value = [JddCommonFunction getNumberByStrDXDS:((UIButton*)inView).titleLabel.text];
            if (inView.tag == 66 && [lotteryNumber Ball6HasNumber:value]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }else if (inView.tag == 77 && [lotteryNumber Ball7HasNumber:value]){
                [((UIButton*)inView) setSelected:YES];
                [JddCommonFunction animationViewWithZoomOutIn:inView delaySecond:0];
            }
            }
        }
    }
    [self countMoney];
}

- (IBAction)doneButtonClick:(id)sender {
    if ([[JddPickNumCQSSListViewController sharedInstance].lotNumListTable numberOfRowsInSection:0]>0) {
        if ([moneyLabel.text isEqualToString:@"0彩豆"]) {
            isQuit = FALSE;
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_CQSS_LIST fromController:self withObject:nil withObject2:nil];
        } else {
            isQuit = FALSE;
            [self pushList];
        }
    } else {
    if ([moneyLabel.text isEqualToString:@"0彩豆"]) {
        NSString *tip = [self getTip];
        [JddCommonFunction messageBoxWithMessage:tip];
    }else{
        isQuit = FALSE;
        [self pushList];
        }
    }
}

- (IBAction)randomOne:(id)sender {
    [self yaohao];
}

-(void)pushList
{
    [lotteryNumArray removeAllObjects];
    JddFc3dNumber *newNum = [[JddFc3dNumber alloc] init];
    [newNum.BallArray addObjectsFromArray:lotteryNumber.BallArray];
    [newNum.SecondBallArray addObjectsFromArray:lotteryNumber.SecondBallArray];
    [newNum.ThirdBallArray addObjectsFromArray:lotteryNumber.ThirdBallArray];
    [newNum.FourthBallArray addObjectsFromArray:lotteryNumber.FourthBallArray];
    [newNum.FifthBallArray addObjectsFromArray:lotteryNumber.FifthBallArray];
    [newNum.SixthBallArray addObjectsFromArray:lotteryNumber.SixthBallArray];
    [newNum.SeventhBallArray addObjectsFromArray:lotteryNumber.SeventhBallArray];
    [lotteryNumArray addObject:newNum];
    NSString *name = [NSString stringWithFormat:@"%@%@", [playName objectAtIndex:0], [playName objectAtIndex:1]];
    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_CQSS_LIST fromController:self withObject:lotteryNumArray withObject2:name];
}

-(NSString *)getTip
{
    NSString *name = [NSString stringWithFormat:@"%@%@", [playName objectAtIndex:0], [playName objectAtIndex:1]];
    NSString *str = [NSString stringWithFormat:@"%@每一区域内至少需要选择一位!", name];
    NSString *firstPlayName = [playName objectAtIndex:0];
    NSString *secondPlayName = [playName objectAtIndex:1];
    if ([firstPlayName isEqualToString:@"一星"]){
        str = @"至少选择1位号码";
    }
    if ([secondPlayName isEqualToString:@"组选"]){
        str = @"至少选择2位号码";
    }
    return str;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [MobClick endLogPageView:@"CQSSC"];
    
    if (isQuit) {
        [countDownTimer invalidate];
    }
    [UIAccelerometer sharedAccelerometer].delegate = nil;
    sharked = true;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"CQSSC"];
	[MobClick event:@"Lottery-CQSSC"];
    
    isQuit = YES;
    //监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startQuery) name:CQSS_MESSAGE object:nil];
    //重置
    [self clearButtonClick:nil];
    sharked = false;
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0];
    
    
    shakeCount = 0;
    [self setScroViewContentOffsetWithView:defaultBg];
    [self startQuery];
    [self randomOne:nil];
}
#pragma mark 起始玩法
-(void)setScroViewContentWithHeight:(CGFloat)height
{
    [sc setContentSize:CGSizeMake(sc.frame.size.width, height)];
    CGRect frame = sc.frame;
    frame.size.height = SCREEN_HEIGHT - 157;
    [sc setFrame:frame];
    //默认滚动到三星玩法
    defaultBg = bg2;
}
#pragma mark  要改的地方
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    TSLocateView *locateView = (TSLocateView *)actionSheet;
  //  locateView.backgroundColor = [UIColor whiteColor];
    TSLocation *location = locateView.locate;
  //  NSLog(@"city:%@-%@", location.state, location.city);
    //You can uses location to your application.
    if(buttonIndex == 0) {
        NSLog(@"Cancel");
    }else {
        NSLog(@"Select");
        
        NSString * firstString = location.state;
        NSString * subString = location.city;
        
        playName = [NSArray arrayWithObjects:firstString, subString, nil];
        NSString *str = [NSString string];
        self.label5.text = @"个位";
        //大小单双
        if ([firstString isEqualToString:@"大小单双"]) {
            str = @"从个、十位中任选一种性质组成一注投注号码、奖金4彩豆";
            //滚动
            [self setScroViewContentOffsetWithView:bg3];
            //显示大小单球，隐藏其它种类
            [self switchDXDSAndStartWithIsHidden:YES];
        }
        else{
            //显示其它球种，隐藏大小单双
            [self switchDXDSAndStartWithIsHidden:NO];
            BOOL isFS = NO;
            if ([subString isEqualToString:@"复选"]) {
                isFS = YES;
            }
            if ([firstString isEqualToString:@"五星"]) {
                if ([subString isEqualToString:@"直选"]) {
                    str= @"选号与开奖号码全部相同且顺序一致，奖金100000彩豆。";
                }else if ([subString isEqualToString:@"通选"]) {
                    str= @"一注号码，三个奖级通吃，五次中奖机会，大奖20440彩豆。";
                }else if (isFS) {
                    str= @"包括五星、三星、二星、一星各投一注，最高奖金达101110（为五星奖金+三星奖金+二星奖金+一星奖金）彩豆。";
                }
                //滚动
                [self setScroViewContentOffsetWithView:bg1];
                sc.scrollEnabled = YES;
            }else if ([firstString isEqualToString:@"三星"]){
                if ([subString isEqualToString:@"直选"]) {
                    str= @"选号与开奖号码全部相同且顺序一致，奖金1000彩豆。";
                }else if (isFS) {
                    str= @"包括三星、二星、一星各投一注，最高奖金达1110彩豆。";
                }
                //滚动
                [self setScroViewContentOffsetWithView:bg2];
            }else if ([firstString isEqualToString:@"二星"]){
                if ([subString isEqualToString:@"直选"]) {
                    str= @"选号与开奖号码全部相同且顺序一致，奖金100彩豆。";
                }else if (isFS) {
                    str= @"包括二星、一星各投一注，最高奖金达110彩豆。";
                }
                if ([subString isEqualToString:@"组选"]) {
                    str= @"猜开奖号码的最后两位，选择2个或以上号码投注，奖金50彩豆。";
                    //滚动
                    [self setScroViewContentOffsetWithView:bg4];
                    self.label5.text = @"个位";
                }else{
                    //滚动
                    [self setScroViewContentOffsetWithView:bg3];
                }
            }else if ([firstString isEqualToString:@"一星"]){
                str = @"选号与开奖号码全部相同且顺序一致，奖金10彩豆";
                //滚动
                [self setScroViewContentOffsetWithView:bg4];
            }
        }
        playIDTips.text = str;
        playIDnameLabel.text = [NSString stringWithFormat:@"%@%@", firstString, subString];
        [self clearButtonClick:nil];
        [self setRbtnTitle:[NSString stringWithFormat:@"%@-%@",PAGE_CQSS_PICK_NUMBER,playIDnameLabel.text]];
    }
}



-(void)initData
{
    playName = [NSArray arrayWithObjects:@"三星" ,@"直选", nil];
    playIDnameLabel.text = @"三星直选";
}
- (void)loadNavBar
{
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(0, 0, 40, 30);
    [right setTitle:@"帮助" forState:UIControlStateNormal];
    right.titleLabel.font = [UIFont systemFontOfSize:15];
    [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [right addTarget:self action:@selector(listSelect) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    
}
//- (void)backView
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化scroView
    [self setScroViewContentWithHeight:490];
    //初始化数值
    [self initData];
//    [self.navigationItem setTitle:PAGE_CQSS_PICK_NUMBER];
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_CQSS_PICK_NUMBER];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;

    [self loadNavBar];
    currentLotteryIssue = [[JddLotteryIssue alloc] init];
    
    lotteryNumber = [[JddFc3dNumber alloc] init];
    lotteryNumArray = [NSMutableArray array];
    
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0];
   

    //添加下拉选择
    [self addMenuBtn];
}
-(void)addMenuBtn
{
    NSString *title = [NSString stringWithFormat:@"%@-三星直选",PAGE_CQSS_PICK_NUMBER];
    
    rv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-140, 30)];
    rBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rBtn addTarget:self action:@selector(selectPlayMethodButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [rBtn setTitle:title forState:UIControlStateNormal];
    rBtn.titleLabel.font = [UIFont systemFontOfSize: 19.0];
    [rBtn setFrame:CGRectMake(0, 2, 180, 19)];
    rBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    UIImage *rBtnImg = LOADIMAGE(@"navBar_down");
    rImv = [[UIImageView alloc] initWithImage:rBtnImg];
    [self setRightBtnImage:title];
    
    //创建的一个view，加入btn和图片
    [rv addSubview: rBtn];
    [rv addSubview:rImv];
    self.navigationItem.titleView = rv;
    
    //加入手势
    rImv.userInteractionEnabled = YES;
    UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPlayMethodButtonClick)];
    [rImv addGestureRecognizer:recognizer];
}

//计算rImv的坐标
-(void)setRightBtnImage:(NSString *)titleStr
{
    JddSportNumber *btntitle = [[JddSportNumber alloc] init];
    btntitle.titleStr = titleStr;
    CGFloat width = [btntitle customWidth:19 font:19];
    rBtn.center = CGPointMake(90, 15);
    CGRect frame = rBtn.frame;
    frame.origin.x = (rv.width - width - 23) / 2;
    frame.size.width = width;
    rBtn.frame = frame;
    [rImv setFrame:CGRectMake(rBtn.frame.origin.x + width + 3, 9, 20, 12)];
}

//选择彩种
-(void)setRbtnTitle:(NSString *)title
{
    [rBtn setTitle:title forState:UIControlStateNormal];
    [self setRightBtnImage:title];
}

- (void)viewDidUnload {
    sharked = TRUE;
    [self setIssueCodeLabel:nil];
    [self setRemainTimeLabel:nil];
    [self setPlayIDTips:nil];
    [self setLabel1:nil];
    [self setLabel2:nil];
    [self setLabel3:nil];
    [self setZhushuLabel:nil];
    [self setMoneyLabel:nil];
    [self setPlayIDnameLabel:nil];
    [super viewDidUnload];
}




@end
